﻿Imports Microsoft.VisualBasic
Imports JPC.Medialibrary.DAL

Namespace JPC.Medialibrary.BLL

    Public Class CurrentPlayListManager

        Public Shared Sub AddSongToCurrentPlaylist(ByVal songId As Integer)
            AddSongToCurrentPlaylist(Song.FetchByID(songId))
        End Sub

        Public Shared Sub AddSongToCurrentPlaylist(ByVal song As Song)
            Dim cpl As currentPlaylistSongs = MySession.currentPlaylistSongs
            Dim n As Integer
            For n = 1 To cpl.Count
                If cpl(n - 1).SongId = song.SongId Then Exit For
            Next
            If n > cpl.Count Then
                cpl.Add(song)
            End If
            MySession.currentPlaylistSongs = cpl
        End Sub

        Public Shared Sub AddAlbumToCurrentPlaylist(ByVal albumId As Integer)
            Dim albumSongs As List(Of Song) = MedialibraryManager.GetSongsByAlbum(albumId)
            For Each _song As Song In albumSongs
                AddSongToCurrentPlaylist(_song)
            Next
        End Sub

        Public Shared Sub AddArtistToCurrentPlaylist(ByVal artistId As Integer)
            Dim artistSongs As List(Of Song) = MedialibraryManager.GetSongsByArtist(artistId)
            For Each _song As Song In artistSongs
                AddSongToCurrentPlaylist(_song)
            Next
        End Sub

        Public Shared Sub AddAlbumArtistToCurrentPlaylist(ByVal albumId As Integer, ByVal artistId As Integer)
            Dim albumSongs As List(Of Song) = MedialibraryManager.GetSongsByAlbumArtist(albumId, artistId)
            For Each _song As Song In albumSongs
                AddSongToCurrentPlaylist(_song)
            Next
        End Sub

        Public Shared Sub AddAlbumTagToCurrentPlaylist(ByVal albumId As Integer, ByVal tagId As Integer)
            Dim albumSongs As List(Of Song) = MedialibraryManager.GetSongsByAlbumTag(albumId, tagId)
            For Each _song As Song In albumSongs
                AddSongToCurrentPlaylist(_song)
            Next
        End Sub

        Public Shared Sub AddPlayListToCurrentPlaylist(ByVal playlistId As Integer)
            Dim playlistSongs As List(Of Song) = MedialibraryManager.GetSongsByPlaylist(playlistId)
            For Each _song As Song In playlistSongs
                AddSongToCurrentPlaylist(_song)
            Next
        End Sub

        Public Shared Function Remove2(ByVal songId As Integer) As String
            Dim returnAction As String = ""
            For Each _song In MySession.currentPlaylistSongs
                If _song.SongId = songId Then
                    If Not MySession.SelectedSong Is Nothing Then
                        If MySession.SelectedSong.SongId = songId Then
                            returnAction = "Stop"
                        End If
                    End If
                    MySession.currentPlaylistSongs.Remove(_song)
                    Exit For
                End If
            Next
            Return returnAction
        End Function

        Public Shared Function ClearCurrentPlaylist2() As String
            Dim returnAction As String = ""
            MySession.currentPlaylistSongs = New currentPlaylistSongs()
            If MySession.isPlayingFromPlaylist Then
                returnAction = "Stop"
            End If
            Return returnAction
        End Function

        Public Shared Sub MoveUp(ByVal songId As Integer)
            Dim n As Integer = 0
            For Each _song In MySession.currentPlaylistSongs
                If _song.SongId = songId Then
                    MySession.currentPlaylistSongs.Reverse(n - 1, 2)
                    Exit For
                End If
                n += 1
            Next
        End Sub

        Public Shared Sub MoveDown(ByVal songId As Integer)
            Dim n As Integer = 0
            For Each _song In MySession.currentPlaylistSongs
                If _song.SongId = songId Then
                    MySession.currentPlaylistSongs.Reverse(n, 2)
                    Exit For
                End If
                n += 1
            Next
        End Sub

        Public Shared Sub SelectCurrentSong(ByVal songId As Integer)
            Dim _song As New Song
            _song.LoadByKey(songId)
            MySession.SelectedSong = _song
        End Sub

        Public Shared Function Previous2() As Integer
            Dim _songId As Integer = 0
            Dim n As Integer = 0
            For Each _song As Song In MySession.currentPlaylistSongs
                If _song.SongId = MySession.SelectedSong.SongId And n > 0 Then
                    _songId = MySession.currentPlaylistSongs(n - 1).SongId
                    Exit For
                End If
                n += 1
            Next
            Return _songId
        End Function

        Public Shared Function [Next2]() As Integer
            Dim _songId As Integer = 0
            Dim n As Integer = 0
            For Each _song As Song In MySession.currentPlaylistSongs
                If _song.SongId = MySession.SelectedSong.SongId And n < MySession.currentPlaylistSongs.Count - 1 Then
                    _songId = MySession.currentPlaylistSongs(n + 1).SongId
                    Exit For
                End If
                n += 1
            Next
            Return _songId
        End Function

    End Class

End Namespace
